#include <stdio.h>
#include "tree.h"
#include "sline.h"
#include "string.h"
// 先序遍历
void preorder(T_node* node)
{
    if(node != NULL)
    {
        sl_printf("%d\n",node->data);
        preorder(node->left);
        preorder(node->right);
    }
}
// 中序遍历 左->根->右
void inorder(T_node* node)
{
    if(node != NULL)
    {
        inorder(node->left);
        sl_printf("%d\n",node->data);
        inorder(node->right);
    }
}

// 后遍历 
void postorder(T_node* node)
{
    if(node != NULL)
    {
        postorder(node->left);
        postorder(node->right);
        sl_printf("%d\n",node->data);
    }
}

// 层次遍历 
void levelorder(T_node* node)
{
    T_node* list[128] = {};
    T_node* list_b[128] = {};
    int node_num = 0;
    int node_num_b = 0;

    if(node != NULL)
    {
        list[node_num] = node;
        node_num++;
    }

    while (node_num != 0)
    {
        for(int i = 0;i<node_num;i++)
        {
            sl_printf("%d\n",list[i]->data);
            if(list[i]->left!=NULL)
            {
                // sl_printf("add left %d\n",i);
                list_b[node_num_b] = list[i]->left;
                node_num_b++;
            }
            if(list[i]->right!=NULL)
            {
                // sl_printf("add right %d\n",i);
                list_b[node_num_b] = list[i]->right;
                node_num_b++;
            }
        }
        node_num = node_num_b;
        sl_printf("node_num = %d\n",node_num);
        node_num_b = 0;
        memcpy(list,list_b,128*sizeof(T_node*));
    } 
}



void tree_test(void)
{
    T_node n1,n2,n3,n4,n5;

    n1.data = 5;
    n2.data = 6;
    n3.data = 7;
    n4.data = 8;
    n5.data = 9;

    n1.left  = NULL;
    n1.right = NULL;
    n2.left  = NULL;
    n2.right = NULL;
    n3.left  = NULL;
    n3.right = NULL;
    n4.left  = NULL;
    n4.right = NULL;
    n5.left  = NULL;
    n5.right = NULL;

    n1.left  = &n2;
    n1.right = &n3;
    n2.left  = &n4;
    n4.right  = &n5;

    preorder(&n1);
    sl_printf("max = %d\n",get_maxmum(&n1));
}



